<!--结构-->
<template>
  <div class="login_container">
    <div class="leftText">VUE电商项目</div>
    <div class="rightText">欢迎使用熊先森系统！</div>
    <div class="bgcImg">
      <img class="img1" src="../assets/1.png" alt />
      <!-- login区域 -->
      <div class="login_box">
        <!-- logo区域 -->
        <div class="avatar_box">
          <img src="../assets/logo.png" alt />
        </div>
        <div class="title">熊先森</div>
        <!-- form表单区域 -->
        <el-form
          class="Form"
          label-width="0px"
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginFormRules"
        >
          <!-- 用户名及密码区域 -->
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="用户名"
              prefix-icon="el-icon-user-solid"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" placeholder="密码" prefix-icon="el-icon-lock"></el-input>
          </el-form-item>
          <!-- 按钮区域 -->
          <div>
            <el-form-item>
              <el-button class="loginBtn" type="primary" @click="login">立即登陆</el-button>
            </el-form-item>
            <el-form-item>
              <el-button class="resetBtn" type="info" @click="resetLoginForm">立即重置</el-button>
            </el-form-item>
          </div>
        </el-form>
      </div>
    </div>
    <div id="footerText">
      <div>2020.7.17</div>
      <div>http://www.xiongxiansen.com</div>
    </div>
  </div>
</template>

<!--行为-->
<script>
export default {
  data() {
    return {
      // 这是登录表单的数据绑定对象
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 这是表单的验证规则对象
      loginFormRules: {
        // 验证用户名是否合法
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 点击重置按钮，重置登录表单
    resetLoginForm() {
      // console.log(this);
      this.$refs.loginFormRef.resetFields()
    },
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        // 发送请求进行登录
        const { data: res } = await this.$http.post('login', this.loginForm)
        // console.log(res)
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.$message.success(res.meta.msg)
        // 保存token
        window.sessionStorage.setItem('token', res.data.token)
        // 导航到home主页
        this.$router.push('/home')
      })
    }
  }
}
</script>

<!--样式-->
<style lang="less" scoped>
.login_container {
  // 登陆界面整体背景色
  //   background-color: #0073ea;
  height: 100%;
}
.leftText {
  font-size: 28px;
  color: #f65c2d;
  padding-top: 30px;
  margin-left: 30px;
  font-weight: 700;
}
.rightText {
  font-size: 14px;
  color: gray;
  position: absolute;
  right: 20px;
  top: 60px;
}
.bgcImg {
  width: 100%;
  height: 560px;
  position: absolute;
  top: 80px;
  .img1 {
    width: 100%;
    height: 100%;
  }
}
.Form {
  padding: 40px 20px 20px;
}
.title {
  text-align: center;
  margin-top: 30px;
  font-weight: 700;
  font-size: 20px;
  color: #36af34;
  font-family: 'Microsoft Yahei';
}
.login_box {
  width: 360px;
  height: 400px;
  background-color: #fff;
  border-radius: 5px;
  position: absolute;
  top: 60%;
  right: 10%;
  transform: translate(0, -60%);
  .avatar_box {
    width: 50px;
    height: 50px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 5px;
    position: absolute;
    top: -18px;
    right: 330px;
    box-shadow: 0 0 10px #ddd;
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.loginBtn,
.resetBtn {
  width: 100%;
}
#footerText {
  position: absolute;
  bottom: 10px;
  right: 50%;
  transform: translate(50%,0);
  color: gray;
  text-align: center;
  font-size: 12px;
}
</style>
